package com.srd.phone.mvp.presenters;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.srd.phone.application.HttpStringRequest;
import com.srd.phone.bean.BarrioInfo;
import com.srd.phone.dialog.BarrioDialog;
import com.srd.phone.mvp.interfaces.IBaseView;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TabBorrowPeoplePresenter extends BasePresenter {

    public TabBorrowPeoplePresenter(RequestQueue requestQueue0, IBaseView iView0) {
        super(requestQueue0, iView0);
    }

    /**
     * 网络请求：短信验证码发送
     */
    public void requestMSMCode(final String url, final String value) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("value", value);
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    /**
     * 网络请求：
     */
    public void requestGet(final String url) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        });
        request.setTag(url);
        requestQueue.add(request);
    }

    /**
     * 网络请求：
     */
    public void requestData(final String url, final Map<String, String> map) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        request.setTag(url);
        requestQueue.add(request);
    }

    /**
     * 网络请求：
     */
    public void requestData(final String url) {
        HttpStringRequest request = new HttpStringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                iView.get().returnData(url, response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                iView.get().onError(url, error);
            }
        });
        request.setTag(url);
        requestQueue.add(request);
    }

    public String checkEmpty(String name, String card, String phone, String checkNum, String money, String sketch) {
        if (TextUtils.isEmpty(name)) {
            return "姓名不能为空";
        }
        if (TextUtils.isEmpty(card)) {
            return "身份证号不能为空";
        }
        if (!checkCard(card)) {
            return "身份证号格式不对";
        }
        if (TextUtils.isEmpty(phone))
            return "手机号码不能为空";
        if (TextUtils.isEmpty(checkNum))
            return "验证码不能为空";
        if (TextUtils.isEmpty(money))
            return "借款金额不能为空";
        if (TextUtils.isEmpty(sketch))
            return "借款用途简述不能为空";
        return "";
    }

    /**
     * 验证手机号
     */
    public boolean checkPhone(String phone) {
        if (TextUtils.isEmpty(phone) || phone.length() != 11)
            return false;
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(phone);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 验证身份证号
     */
    public boolean checkCard(String value) {
        String strPattern = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(value);
        boolean b = m.matches();
        return b;
    }

    /**
     * 获取省份对象
     *
     * @param id
     * @return
     */
    public BarrioInfo getProviceValue(String id) {
        int value = Integer.parseInt(id);
        BarrioInfo info = null;
        for (BarrioInfo barrioInfo : BarrioDialog.mProvinceDatas) {
            if (value == barrioInfo.id) {
                info = barrioInfo;
                break;
            }
        }
        return info;
    }

    /**
     * 获取城市对象
     *
     * @param id
     * @return
     */
    public BarrioInfo getCityValue(String proviceId, String cityId) {
        int proviceValue = Integer.parseInt(proviceId);
        int cityValue = Integer.parseInt(cityId);
        BarrioInfo info = null;
        for (BarrioInfo barrioInfo : BarrioDialog.mCitisDatasMap.get(proviceValue)) {
            if (cityValue == barrioInfo.id) {
                info = barrioInfo;
                break;
            }
        }
        return info;
    }

    /**
     * 获取市区对象
     *
     * @param id
     * @return
     */
    public BarrioInfo getDistrictValue(String cityId, String districtId) {
        int cityValue = Integer.parseInt(cityId);
        int districtValue = Integer.parseInt(districtId);
        BarrioInfo info = null;
        for (BarrioInfo barrioInfo : BarrioDialog.mDistrictDatasMap.get(cityValue)) {
            if (districtValue == barrioInfo.id) {
                info = barrioInfo;
                break;
            }
        }
        return info;
    }

}
